#include "highgui.h"
#include "cv.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <FL/Fl_File_Chooser.H>
#include "Utils.hpp"

#define RECTANGLE 1
#define TRIANGLE 2
#define CIRCLE 3

using namespace std;

void mouseCallback(int event, int x, int y, int flags, void* param);
CvRect box;
bool drawingShape = false;
bool drawShape = true;
bool deformingPic = false;
bool deformPicture = false;
int chosenShape;
vector<int> xs,ys;



int main( int argc, char** argv ) {

	//opens a file chooser windows
	Fl_File_Chooser fileChooser(".","*.jpg",0,"File Chooser");
	fileChooser.show();
	while( fileChooser.shown() ) { Fl::wait(); }

	IplImage* img = cvLoadImage(fileChooser.value(),1);
	//makes the file chooser windows disappear
	fileChooser.rescan();

	cvNamedWindow("Original Picture", CV_WINDOW_AUTOSIZE);
	cvShowImage( "Original Picture", img );

	chosenShape = 0;
	//keeps asking the user for a desired shape until the shape is legal (1-3)
	while (chosenShape < 1 || chosenShape > 3) {
		cout << "What shape would you like to use for the filter ? \n"
				"1 - Rectangle\n"
				"2 - Triangle\n"
				"3 - Circle" << endl;
		cin >> chosenShape;
	}

	switch (chosenShape) {
		case RECTANGLE: {
			cout << "Click 2 times on the picture: first on the left highest point\n"
					"and second on the right lowest point" << endl;
		}
		break;
		case TRIANGLE: {
			cout << "Click 3 times on the picture: first select the center of the triangle, second and third clicks\n"
					"will determine the size of each side in the Equilateral Triangle" << endl;
		}
		break;
		case CIRCLE: {
			cout << "Click 3 times on the picture: first select the center of the circle, second and third clicks\n"
					"will determine the size of the radius" << endl;

		}
		break;
	}

	box = cvRect(-1,-1,0,0);
    IplImage* temp = cvCloneImage( img );
    cvNamedWindow( "Original Picture" );

    cvSetMouseCallback("Original Picture",mouseCallback,(void*) img);

    while( 1 ) {
      cvCopyImage( img, temp );
      if( drawingShape ) {
  //  	  Utils::draw_grid(temp,numOfRows,numOfCols,box);
      }
      cvShowImage( "Original Picture", temp );
      if (cvWaitKey( 15 )==27) {
    	  break;
      }
    }

    cvReleaseImage( &img );

    cvReleaseImage( &temp );
    cvDestroyWindow( "Original Picture" );
}


void mouseCallback (int event, int x, int y, int flags, void* param) {
	IplImage* image = (IplImage*) param;

	if (drawShape) {

		switch( event ) {
		  case CV_EVENT_LBUTTONDOWN: {
			  switch (chosenShape) {
				case RECTANGLE: {
					if (xs.size() != 2 && ys.size() != 2) {
						xs.push_back(x);
						ys.push_back(y);
					} else {
						drawShape = false;
						Utils::drawRect(image, xs.at(0), ys.at(0), xs.at(1), ys.at(1));
					}
				}
				break;
				default: { //TRIANGLE or CIRCLE
					if (xs.size() != 3 && ys.size() != 3) {
						xs.push_back(x);
						ys.push_back(y);
					} else {
						drawShape = false;
						if (chosenShape == TRIANGLE) {
							//TODO draw TRIGNALE
						} else {
							//TODO draw CIRCLE
						}
					}
				}
				break;
			}
		  }
		  break;
	   }

	} /*else if (deformPicture && box.x<=x && x<=box.x+box.width) {

		switch( event ) {
		  case CV_EVENT_MOUSEMOVE: {
			if( deformingPic ) {
				IplImage* temp = cvCloneImage( image );
				Utils::deformPic(image,temp,box,abs(xs.at(0) - xs.at(1)), xs.at(0) < xs.at(1) ? true : false, interpolationMethod);
				cvNamedWindow("Deforming Image", CV_WINDOW_AUTOSIZE);
				cvShowImage( "Deforming Image", temp );
                cvReleaseImage( &temp );
				xs.pop_back();
				xs.push_back(x);
			}
		  }
		  break;
		  case CV_EVENT_LBUTTONDOWN: {
			deformingPic = true;
			xs.push_back(x);
			xs.push_back(x);
		  }
		  break;
		  case CV_EVENT_LBUTTONUP: {
			deformingPic = false;
			deformPicture = false;
			cout << "d: " << abs(xs.at(0) - xs.at(1)) << endl;

		  }
		  break;
	   }
	}*/
}
